$import("com.joypard.platform.log.loginlog.LoginLogDI");

K.defView("com.joypard.platform.log.loginlog.LoginLogView", {

	initialize : function() {

	},
	purviewConfig : {

	},
	/**
	 * 显示相关组件到界面
	 */
	render : function() {
		this.logSearchPanel = this.createLogSearchPanel();// 日志搜索面板
		this.logGridPanel = this.createLogGridPanel(); // 日志列表面板

		this.MP = new K.Ext.MainPanel({
			items : [ this.logSearchPanel, this.logGridPanel ]
		});
	},

	/**
	 * 创建日志搜索面板
	 */
	createLogSearchPanel : function() {
		var options = {
			title : "条件查询",
			region : "north",
			frame : true,
			margins : "2 2 2 2",
			iconCls : "p-search",
			collapsible : true,
			bodyCssCls : "search-gb",
			contentEl : "loginLog-search"
		};
		var me = this;

		$("#loginLog-search").keypress(function(evt) {
			var tagName = evt.target.tagName;
			if (tagName == "INPUT" && evt.keyCode == Ext.EventObject.ENTER) {
				me.onSearchLog();
			}
		});

		return new K.Ext.Panel(options);
	},

	/**
	 * 创建日志的列表面板
	 */
	createLogGridPanel : function() {
		var errorImage = "<img src='" + $res_path_resources + "/images/state/error.png" + "'  title='失败' />";
		var successImage = "<img src='" + $res_path_resources + "/images/state/success.png" + "' title='成功' />";
		var me = this;
		var sm = new Ext.grid.CheckboxSelectionModel({
			listeners : {
				rowselect : function(sm, rowIndex, r) {}
			}
		});
		var options = {
			title : "登录日志列表",
			iconCls : "p-grid",
			borderStyle : "boder-left:0px;border-right:0px;border-bottom:0px;",
			region : "center",
			margins : "0 2 2 2",
			viewConfig : {
			// forceFit : true
			}, // 自动分配列宽
			store : LoginLogDI.createLogStore(),
			tbar : [ {
				text : "查询",
				iconCls : 'search',
				handler : function() {
					me.onSearchLog();
				}
			} ],
			sm : sm,
			columns : [ sm, {
				header : "状态",
				width : 55,
				dataIndex : "state",
				renderer : function(value) {
					if (value == "1") {
						return successImage;
					} else {
						return errorImage;
					}
				}
			}, {
				header : "编号",
				width : 40,
				dataIndex : "userCode"
			}, {
				header : "登录帐号",
				width : 80,
				dataIndex : "account"
			}, {
				header : "姓名",
				width : 80,
				dataIndex : "userName"
			}, {
				header : "IP",
				width : 120,
				dataIndex : "ip"
			}, {
				id : "loginTime",
				header : "登录时间",
				width : 150,
				dataIndex : "loginTime",
				renderer : function(value) {
					if (value instanceof Date) {
						return new Date(value).format("Y-m-d");
					} else {
						return value;
					}
				}
			} ],
			autoExpandColumn : "loginTime",
			bbar : {
				xtype : "pagination"
			},
			listeners : {
				afterrender : function() {
					this.getStore().load();
				}
			}
		};
		return new K.Ext.grid.GridPanel(options);
	},

	/**
	 * 查询日志事件
	 */
	onSearchLog : function() {
		var params = K.serializeJson("#loginLog-search");
		this.logGridPanel.getStore().load({
			params : params
		});
	}
});